testStage = JSGF.inherit(JSGF.Stage);

testStage.prototype.onLoad = function() {
	var self = this;
	
	this.blockSize = {'w':50, 'h':25};
	this.blockPerLine = 10;
	this.num = 1;
	this.keyConfig = {'left':37, 'right':39};
	this.kd  = 0;
	this.slist = new Array();

	this.mapData = [
		1,1,1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,1,1,
	];

	this.bar = JSGF.factory(JSGF.Sprite, ["image/bar.png"], 100,25);
	this.bar.move(Math.floor(this.getWidth() / 2), 400);
	this.bar.v = {'l':-5, 'r':5, 'current':0};
	this.bar.mvHelical = function(v){
		var x = this.getX();
		if( (x+v>=0) && (x+v<=self.getWidth()-this.getWidth()) ){this.move(x+v,this.getY());}
	};
	
	
	this.appendSprite(this.bar);
	this.ball = JSGF.factory(JSGF.Sprite, ["image/blue1.png", "image/blue2.png", "image/blue3.png", "image/blue4.png", ], 20,20);
	this.ball.v = {'x': 2 , 'y':2, 'maxX':4, 'maxY':4};
	this.ball.reflectX = function(){this.v.x *= -1;}
	this.ball.reflectY = function(){this.v.y *= -1;}
	this.ball.reflectBar = function(){
		if(this.v.y>0){ this.v.y *= -1;}
		if(self.bar.v.current>0){
			this.v.x += 1;
		}else{
			this.v.x -=1;
		}
		if( Math.abs(this.v.x) > this.v.maxX ){
			if(	this.v.x < 0 ){ this.v.x = this.v.maxX*(-1); }else{ this.v.x = this.v.maxX; }
		}
	}

	//ball.setWidth(ball.getWidth()/5);
	//ball.setHeight(ball.getHeight()/5);
	//ball.move(Math.floor(this.getWidth() / 2),Math.floor(this.getHeight() / 2));
	this.ball.move(10,Math.floor(this.getHeight() / 2));
	
	this.ball.startAnimation(100);
	this.appendSprite(this.ball);
	this.ball.onLoad = function (s){
		//s.setWidth(100);
		//s.setHeight(100);
	//	s.move(Math.floor(self.getWidth() / 2),Math.floor(self.getHeight() / 2));
	}

	for (var i = 0; i < this.mapData.length; i++) {
		var imageFile = ["image/block.png" ]
		var sprite = JSGF.factory(JSGF.Sprite, imageFile);
		//var sprite = JSGF.factory(JSGF.Sprite, "image/blue.png");
		//sprite.move(Math.floor(Math.random() * 600) - 100, Math.floor(Math.random() * 600) - 100);
		if(this.mapData[i]!=0){
			sprite.move(this.blockSize.w*(i%this.blockPerLine), this.blockSize.h*Math.floor(i/this.blockPerLine));
		}
		sprite.setZIndex(i);
		sprite.startAnimation(100);

		this.appendSprite(sprite);
		this.slist.push(sprite);
	}

	this.testtext = JSGF.factory(JSGF.SpriteText, "テスト");
	this.testtext.setZIndex(10000);
	this.testtext.color = "black";
	this.testtext.height = 32;
	this.testtext.move(100, 100);
	this.appendSprite(this.testtext);

//	setTimeout(function(){
//		for (var i = 0; i < self.slist.length; i++) {
//			self.slist[i].stopAnimation();
//		}
//			//self.removeSprite(self.ball);
//	},1000);

	this.drawUpdate();

	this.startAnimation(16);

//	setTimeout(function() {
//		self.stopAnimation();
//	}, 10000);

}

testStage.prototype.onUnload = function() {
	alert("stage unload");
}

testStage.prototype.onDraw = function(dt) {
	var bX = nX = this.ball.getX();var bY = nY = this.ball.getY();
	var bW = this.ball.getWidth();var bH = this.ball.getHeight(); 
	//this.ball.move(bX+this.ball.v.x, bY+this.ball.v.y);
	if( this.bar.isConflict(this.ball) ){
		this.ball.reflectBar();
	}
	if( bX<0 ){
		this.ball.reflectX();
		nX = 0;
	}else if( this.getWidth()<bX+bW ){
		this.ball.reflectX();
		nX = this.getWidth()-bW;
	}else{
		nX = bX+this.ball.v.x;
	}
	
	if( bY<0 ){
		this.ball.reflectY();
		nY = 0;
	}else if( this.getHeight()<bY+bH ){
		this.ball.reflectY();
		nY = this.getHeight()-bH;
	}else{
		nY = bY+this.ball.v.y;
	}
	
	
	for (var i = 0; i < this.slist.length; i++) {//block衝突判定
		var tmpB = this.slist[i];
		var x = tmpB.getX();
		var y = tmpB.getY();

		if(tmpB.isConflict(this.ball)){
			tmpB = null;
		}

	}

	this.ball.move(nX, nY);
	this.bar.mvHelical(this.bar.v.current);
}

testStage.prototype.onKeyDown = function(e) {
	e.keyCode = (e.keyCode)?e.keyCode:e;
	this.kd = e.keyCode;
	this.testtext.setText(e.keyCode+' '+this.bar.v.current);
	switch(true){
		case e.keyCode==this.keyConfig.left:
			this.bar.v.current = this.bar.v.l;
			//this.testtext.setText('left');
			break;
		case e.keyCode==this.keyConfig.right:
			this.bar.v.current = this.bar.v.r;
			//this.testtext.setText('right');
			break;
	}
}

testStage.prototype.onKeyUp = function(e){
	if(e.keyCode === this.kd){	
		this.bar.v.current = 0;
	}
}

testStage.prototype.onNoCanvas = function() {
	//TODO message
}